#方法1 gglot2画图
load("D:/杨熊冰/R语言/实验报告/class5_volcano.RData")
plotdata <- prostat
colnames(plotdata)[2] <- c("log2fc") #修改fc列名
fccutoff <- 1.2; #fc阈值
pcutoff <- 0.05; #p阈值
plotdata$group <- "nosig" #新建一列group 全部设置为nosig
pos = plotdata$log2fc> log2(fccutoff) & plotdata$P<pcutoff
plotdata$group[pos] <- "up" #把满足条件的设置为up
pos = plotdata$log2fc< -log2(fccutoff) & plotdata$P<pcutoff
plotdata$group[pos] <- "down" #把满足条件的设置为down
plotdata$label <- plotdata$ID #新建一列label 用于图上标记信息
plotdata$label[plotdata$group == "nosig"] <- "" #nosig不标记 设置为""
if( !require("ggplot2", quietly = TRUE))
  install.packages("ggplot2")
library(ggplot2)
p <- ggplot(plotdata,aes(x=log2fc,y=-log10(P))) +
  geom_point(aes(color = group))
p #初步绘制
p <- p + scale_color_manual(values = c("green","grey","red"))
p #调整颜色
p <- p + geom_hline(yintercept = -log10(pcutoff),lty=2) + #画横虚线
  geom_vline(xintercept = c(log2(fccutoff),-log2(fccutoff)),lty=2) #画竖虚线
p #加上分割区域的虚线
if( !require("ggrepel", quietly = TRUE))
  install.packages("ggrepel")
library(ggrepel)
p <- p + geom_text_repel(aes(label = label)) 
p #加上标签，只加外围不重叠的区域
library(ggprism)
p <- p + theme_prism()
p #修改主题为prism格式
jpeg("volcano.jpg",width =8, height = 6, units = "in",res = 300) #设置宽高,单位和分辨率
p
graphics.off()
p
